package controller;

import dao.UserDao;
import entity.SmbmsRole;
import entity.SmbmsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author: 举个栗子吖
 * @Date: 2019/11/8 20:45
 */
@Controller
@RequestMapping("/")
@Scope("prototype")
public class UserInfoController {
    @Resource(name = "userDao")
    private UserDao userDao;

//    测试Postman
    @RequestMapping("/list")
    @ResponseBody
    public List<SmbmsRole> list() {
        List<SmbmsRole> arr = userDao.queryAll();
        return arr;
    }


    //    查询 条件查询
    @RequestMapping("/byname")
    @ResponseBody
    public Map<String, Object> ByNameQuery(String userName, Integer offset, Integer limit, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        SmbmsUser user = new SmbmsUser();
        user.setUserName(userName);
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map.put("userName", userName);
        map.put("offset", (offset - 1) * limit);
        map.put("limit", limit);
        List<SmbmsUser> list = userDao.queryByName(map);
        map1.put("total", userDao.count(user));
        map1.put("rows", list);
        return map1;

    }

    // 删除
    @RequestMapping("/del")
    @ResponseBody
    public int del(Integer id) {
        return userDao.del(id);
    }


    //    模态框
    @RequestMapping("/getAll")
    @ResponseBody
    public List<SmbmsRole> getAll(int id) {
//        查询用户当前职位
        SmbmsRole role = userDao.getRoleUserId(id);
//        查询所有职位
        List<SmbmsRole> list = userDao.queryAll();
        for (SmbmsRole r : list) {
            if (role.getId() == r.getId()) {
                r.setIschecked(true);
            }
        }
        return list;
    }

    //    修改用户职位
    @RequestMapping("/doFenPei")
    @ResponseBody
    public int doFenPei(int id, int userRole) {
        return userDao.update(id, userRole);
    }
}
